// pages/classification/classification.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        primary: app.globalData.primary, // 主题色
        ossPath: app.globalData.ossPath,
        allTemplateClass: [], // 所有的分类
        params: {
            ProductCategorieNum: 6,
            ProductTypeId: 14,
            SizeId: '',
            pageIndex: 1,
            pageSize: 16,
            SortOrder: 0,
            ChargingMode: '',
            StrFilterValue: '',
            TeamNum: '',
            keywords: '',
            more: true, // 加载更多时
        },
        templateList: [], // 模板列表
        loading: false, // 加载
        selected: {}, // 选中的分类
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        //设置主题色
        if (app.globalData.primary) {
            this.setData({ primary: app.globalData.primary })
            app.usePrimary(app.globalData.primary)
        }
        
        this.data.params.ProductCategorieNum = options.classnum;
        this.data.params.ProductTypeId = options.typeId;
        this.setData({ params: this.data.params})
        this.getAllTemplateClassification();
        this.setData({ loading: true })
        this.getTemplateCenter().then(data => {
            this.setData({ loading: false })
            data && this.setData({ templateList: data.Data })
        })
    },
    getAllTemplateClassification: function() { // 获取所有的分类
        wx.showLoading()
        wx.request({
            url: app.globalData.serverPath + '/AllTemplateClassification',
            success: ({
                data
            }) => {
                this.setData({
                    allTemplateClass: data
                })
                this.filterClass(data)
            },
            complete: () => {
                wx.hideLoading()
            }
        })
    },
    filterClass: function(data) {
        for(let i = 0; i < data.length; i++) {
            if (data[i].ClassNum == this.data.params.ProductCategorieNum) {
                data[i].ProductTypeList.forEach(item => {
                    item.selected = false;
                    if (item.TypeId == this.data.params.ProductTypeId) {
                        item.selected = true
                    }
                })
                this.data.selected = data[i];
                this.setData({ selected: this.data.selected})
                break;
            }
        }
        data.forEach(item => {
            item.selected = false;
            if (item.ClassNum == this.data.params.ProductCategorieNum) {
                item.selected = true;
            }
        })
    },
    getTemplateCenter: function() { // 获取模板
        
        return new Promise(resolve => {
            var data = '';
            for (let key in this.data.params) {
                data += key + '=' + this.data.params[key] + '&'
            }
            wx.request({
                url: app.globalData.serverPath + '/TemplateCenter',
                method: 'post',
                header: app.globalData.header,
                data: data,
                success: ({
                    data
                }) => {
                    resolve(data)
                },
                fail: err => {
                    resolve(false)
                }
            })
        })
    },
    getMoreTemplate: function() { // 加载更多
        if (!this.data.params.more) return
        this.data.params.pageIndex += 1;
        this.setData({params: this.data.params})
        this.data.params.more = false;
        this.getTemplateCenter().then(data => {
            this.setData({ loading: false })
            this.data.params.more = true;
            if (data) {
                this.data.templateList = [].concat(this.data.templateList, data.Data)
                this.setData({ templateList: this.data.templateList })
            }
            
        })
    },
    changeClassLable: function(e) { // 选择分类
        var index = e.currentTarget.dataset.index;
        this.data.params.pageIndex = 1;
        this.data.selected.ProductTypeList.forEach((item, i) => {
            item.selected = false;
            if (i == index) {
                item.selected = true;
                this.data.params.ProductTypeId = item.TypeId;
            }
        })
        this.setData({
            selected: this.data.selected,
            params: this.data.params,
            loading: true
        })
        this.getTemplateCenter().then(data => {
            this.setData({ loading: false })
            wx.pageScrollTo({
                scrollTop: 0,
                duration: 300
            })
            data && this.setData({ templateList: data.Data })
        })
    },
    gotoDesign: function(e) {
        app.checkedUserInfo().then(ok => {
            if (!ok) return;
            var d = e.currentTarget.dataset.d;
            wx.setStorageSync('d', d)
            wx.setStorageSync('m', 0)
            var data = '?d=' + d
            wx.navigateTo({
                url: '../design/design' + data,
            })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        this.getMoreTemplate();
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})